//
//  CNButton.swift
//  Voice
//
//  Created by 卢逢卫 on 2024/3/13.
//

import UIKit

extension UIButton {

    public func cn_addTarget(events: UIControl.Event = .touchUpInside, block: @escaping () -> Void) {
            if #available(iOS 14.0, *) {
                let action = UIAction { _ in
                    block()
                }
                self.addAction(action, for: events)
            } else {
                self.addTarget(self, action: #selector(executeBlock), for: events)
                objc_setAssociatedObject(self, "cn_block", block, .OBJC_ASSOCIATION_COPY_NONATOMIC)
            }
    }

    @objc private func executeBlock() {
            if let block = objc_getAssociatedObject(self, "cn_block") as? () -> Void {
                block()
            }
    }

    public func cn_setTitleAndImage(title: String?, titleColor: UIColor?, image: UIImage?) {
        self.setTitle(title, for: .normal)
        self.setTitleColor(titleColor, for: .normal)
        self.setImage(image, for: .normal)
    }

    public func cn_setTitle(normal: String, selected: String?) {
        self.setTitle(normal, for: .normal)
        self.setTitle(selected, for: .selected)
    }

    public func cn_setTitleColor(normal: UIColor, selected: UIColor?) {
        self.setTitleColor(normal, for: .normal)
        self.setTitleColor(selected, for: .selected)
    }

    public func cn_setImage(normal: UIImage, selected: UIImage?) {
        self.setImage(normal, for: .normal)
        self.setImage(selected, for: .selected)
    }

    public func cn_setFont(font: UIFont) {
        self.titleLabel?.font = font
    }
}

extension UIButton {

    public func cn_setImageForAllStates(_ image: UIImage) {
        self.setImage(image, for: .normal)
        self.setImage(image, for: .highlighted)
        self.setImage(image, for: .selected)
        self.setImage(image, for: .disabled)
    }

    public func cn_setTitleForAllStates(_ title: String) {
        self.setTitle(title, for: .normal)
        self.setTitle(title, for: .highlighted)
        self.setTitle(title, for: .selected)
        self.setTitle(title, for: .disabled)
    }

    public func cn_setTitleColorForAllStates(_ color: UIColor) {
        self.setTitleColor(color, for: .normal)
        self.setTitleColor(color, for: .highlighted)
        self.setTitleColor(color, for: .selected)
        self.setTitleColor(color, for: .disabled)
    }
}

extension UIButton {

    public func cn_setImageAboveText(imageAboveText: Bool = false, spacing: CGFloat) {
        if imageAboveText {
            self.contentVerticalAlignment = .center
            self.contentHorizontalAlignment = .center
            let imageSize = self.imageView?.frame.size ?? CGSize.zero
            let titleSize = self.titleLabel?.frame.size ?? CGSize.zero
            let totalHeight = imageSize.height + titleSize.height + spacing
            self.imageEdgeInsets = UIEdgeInsets(
                top: -(totalHeight - imageSize.height),
                left: 0,
                bottom: 0,
                right: -titleSize.width
            )
            self.titleEdgeInsets = UIEdgeInsets(
                top: 0,
                left: -imageSize.width,
                bottom: -(totalHeight - titleSize.height),
                right: 0
            )
        } else {
            self.imageEdgeInsets = UIEdgeInsets.zero
            self.titleEdgeInsets = UIEdgeInsets.zero
        }
    }

    public enum CNUIButtonTypes {
        case leftImgRightTitles
        case leftTitleRightImgs
        case topTitleBottomImgs
        case topImgBottomTitles
    }

    public func cn_setBtn(image: UIImage?, title: String, btnType: CNUIButtonTypes, spacing: CGFloat = 0.0, state: UIControl.State = .normal) {
        self.setImage(image, for: state)
        self.setTitle(title, for: state)

        switch btnType {
        case .leftImgRightTitles:
            self.imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing/2, bottom: 0, right: spacing/2)
            self.titleEdgeInsets = UIEdgeInsets(top: 0, left: spacing/2, bottom: 0, right: -spacing/2)
        case .leftTitleRightImgs:
            self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -spacing/2, bottom: 0, right: spacing/2)
            self.imageEdgeInsets = UIEdgeInsets(top: 0, left: spacing/2, bottom: 0, right: -spacing/2)
        case .topTitleBottomImgs:
            self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageEdgeInsets.left, bottom: -spacing, right: -imageEdgeInsets.right)
            self.imageEdgeInsets = UIEdgeInsets(top: -spacing, left: 0, bottom: 0, right: -titleLabel!.bounds.width)
        case .topImgBottomTitles:
            self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -titleLabel!.bounds.height-spacing, right: -titleLabel!.bounds.width)
            self.titleEdgeInsets = UIEdgeInsets(top: -imageView!.bounds.height-spacing, left: -imageView!.bounds.width, bottom: 0, right: 0)
        }
    }
}
